﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace PlanningGIS.MapSheet
{
    /// <summary>
    /// 图幅比例尺对象
    /// </summary>
    [Serializable]
    public class SheetScale
    {
        /// <summary>
        /// 获取或设置图幅比例尺的值，值应该在标准比例尺内
        /// </summary>
        [XmlAttribute]
        public int Value { get; set; }
        
        /// <summary>
        /// 获取或设置比例尺下图幅的格式，不能为空
        /// </summary>
        [XmlAttribute]
        public string Format { get; set; }
        
        /// <summary>
        /// 获取比例尺所在的上级比例尺的值
        /// </summary>
        [XmlAttribute]
        public int ParentValue { get; set; }
        
        /// <summary>
        /// X偏移量
        /// </summary>
        [XmlAttribute]
        public double XOffset { get; set; }
        
        /// <summary>
        /// Y偏移量
        /// </summary>
        [XmlAttribute]
        public double YOffset { get; set; }
        
        /// <summary>
        /// 获取比例尺的显示值
        /// </summary>
        public string Display
        {
            get
            {
                if (Value < 0) return "";
                return string.Format("1:{0}", Value);
            }
        }
        
        /// <summary>
        /// 获取值在标准比例尺中的索引
        /// </summary>
        public int Index
        {
            get
            {
                for (int i = 0; i < ScaleValues.Length; i++)
                {
                    if (ScaleValues[i] == Value)
                        return i;
                }
                return -1;
            }
        }
        
        /// <summary>
        /// 获取比例尺对应的编码
        /// </summary>
        public string Code
        {
            get
            {
                int i = Index;
                if (i < 0) return "";
                return ScaleCodes[i];
            }
        }
        
        /// <summary>
        /// 获取单行图幅的总数
        /// </summary>
        public int SheetCount
        {
            get
            {
                int i = Index;
                if (i < 0) return 0;
                return SheetCounts[i];
            }
        }

        /// <summary>
        /// 判断两个比例尺对象是否相等
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            SheetScale tmp = (SheetScale)obj;
            return this.Value == tmp.Value;
        }

        /// <summary>
        /// 获取对象的Hash码
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Value.GetHashCode();
        }

        /// <summary>
        /// 比例尺数值常量
        /// </summary>
        public static readonly int[] ScaleValues = { 1000000, 500000, 250000, 100000, 50000, 25000, 10000, 5000, 2000, 1000, 500 };
        
        /// <summary>
        /// 比例尺代码常量
        /// </summary>
        public static readonly string[] ScaleCodes = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K" };
        
        /// <summary>
        /// 图幅数量
        /// </summary>
        public static readonly int[] SheetCounts = { 1, 2, 4, 12, 24, 48, 96, 192, 576, 1152, 2304 };

        /// <summary>
        /// 根据比例尺创建对象
        /// </summary>
        /// <param name="value">比例尺</param>
        /// <returns></returns>
        public static SheetScale Create(int value)
        {
            return new SheetScale()
            {
                Value = value
            };
        }

        /// <summary>
        /// 根据比例尺代码（A、B、C...）创建对象
        /// </summary>
        /// <param name="code">比例尺代码（A、B、C...）</param>
        /// <returns></returns>
        public static SheetScale Create(string code)
        {
            int index = -1;
            for (int i = 0; i < ScaleCodes.Length; i++)
            {
                if (!ScaleCodes[i].Equals(code, StringComparison.CurrentCultureIgnoreCase)) continue;
                index = i;
                break;
            }
            if (index < 0) return null;
            return new SheetScale()
            {
                Value = ScaleValues[index]
            };
        }

        /// <summary>
        /// 根据比例尺代码（A、B、C...）获取比例尺
        /// </summary>
        /// <param name="code">比例尺代码（A、B、C...）</param>
        /// <returns></returns>
        public static int GetScaleByCode(string code)
        {
            string s = code.Trim().Substring(0, 1).ToUpper();
            int count = ScaleCodes.Length;
            for (int i = 0; i < count; i++)
            {
                if (!ScaleCodes[i].Equals(s, StringComparison.CurrentCultureIgnoreCase)) continue;
                return ScaleValues[i];
            }
            return -1;
        }
    }
}
